/*
 * @Author: YuWuChen82 1813109877@qq.com
 * @Date: 2025-07-20
 * @LastEditors: YuWuChen82 1813109877@qq.com
 * @LastEditTime: 2025-07-21
 * @FilePath: \antd-demo\src\utils\request.js
 * @Description: 
 */
import axios from 'axios'
import { getToken,removeToken } from '@/utils'
import router from '../router'
const request = axios.create({
    baseURL: 'http://geek.itheima.net/v1_0',
    timeout: 5000
})

request.interceptors.request.use(config => {
    //注入token
    const token = getToken()
    if(token){
        config.headers.Authorization = `Bearer ${token}`
    }
    return config
},error => {
    return Promise.reject(error)
})

request.interceptors.response.use(response => {
    return response.data
},error => {
    if(error.response.status === 401){
       removeToken()
       router.navigate('/login')
    }
   
    return Promise.reject(error)
    
    
})

export default request
